package www.openteam.com.adapter.realtime;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import www.openteam.com.R;
import www.openteam.com.bean.EvaPaperPitBean;
import www.openteam.com.bean.PbKzMsg;
import www.openteam.com.bean.ProductMsg;
import www.openteam.com.tool.AdpUtil;

public class EvaPaperPitAdp extends RecyclerView.Adapter<EvaPaperPitAdp.ViewHolder> {

    Context context;
    List<EvaPaperPitBean> beanList;
    List<PbKzMsg.DataBeanX.DataBean> pitPaperList;
    ArrayAdapter<String> pitNameAdp;


    public EvaPaperPitAdp(Context context) {
        this.context = context;
        beanList = new ArrayList<>();
        EvaPaperPitBean evaPaperPitBean = new EvaPaperPitBean("0","0","0","0","0","0",
                "0","0","0","0");
        beanList.add(evaPaperPitBean);
    }

    public void setPitPaperList(List<PbKzMsg.DataBeanX.DataBean> pitPaperList) {
        this.pitPaperList = pitPaperList;
        List<String> nameList = new ArrayList<>();
        for (PbKzMsg.DataBeanX.DataBean dataBean : pitPaperList) {
            nameList.add(dataBean.getTxt());
        }
        pitNameAdp = new ArrayAdapter<String>(context,
                android.R.layout.simple_spinner_dropdown_item, nameList);
    }

    public List<EvaPaperPitBean> getBeanList() {
        return beanList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_eva_pit_paper, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {




        EvaPaperPitBean evaPaperPitBean = beanList.get(position);
        EvaPaperPitBean bean = new EvaPaperPitBean();
        EvaPaperPitBean baseBean = new EvaPaperPitBean("0","0","0","0","0","0",
                "0","0","0","0");

//        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperPart,s -> bean.setPaperPart(s.toString()),
//                ()->evaPaperPitBean.getPaperPart());
        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperUnit,s -> bean.setPaperUnit(s.toString()),
                ()->evaPaperPitBean.getPaperUnit());
//        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperCode,s -> bean.setPaperCode(s.toString()),
//                ()->evaPaperPitBean.getPaperCode());
//        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperCompose,s -> bean.setPaperCompose(s.toString()),
//                ()->evaPaperPitBean.getPaperCompose());
        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperWidth,s -> bean.setPaperWidth(s.toString()),
                ()->evaPaperPitBean.getPaperWidth());
        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperLength,s -> bean.setPaperLength(s.toString()),
                ()->evaPaperPitBean.getPaperLength());
        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperMaterialNumb,s -> bean.setPaperMaterialNumb(s.toString()),
                ()->evaPaperPitBean.getPaperMaterialNumb());
        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperPrice,s -> bean.setPaperPrice(s.toString()),
                ()->evaPaperPitBean.getPaperPrice());
        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperPitPatter,s -> bean.setPaperPitPatter(s.toString()),
                ()->evaPaperPitBean.getPaperPitPatter());
        AdpUtil.setEditTextWatcher(holder.evaluatePitPaperRemark,s -> bean.setPaperPitPatter(s.toString()),
                ()->evaPaperPitBean.getPaperPitPatter());

        //通过坑纸名称的选择确定坑纸编号和初始的长和宽
        if (pitPaperList!=null){
            holder.evaluatePitPaperCompose.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    PbKzMsg.DataBeanX.DataBean dataBean = pitPaperList.get(i);
                    holder.evaluatePitPaperCode.setText(dataBean.getCode());
                    holder.evaluatePitPaperWidth.setText(dataBean.getWidth());
                    holder.evaluatePitPaperLength.setText(dataBean.getLength());
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
        }

        if (position == beanList.size() - 1) {
            holder.evaluatePitPaperAdd.setVisibility(View.VISIBLE);
        } else {
            holder.evaluatePitPaperAdd.setVisibility(View.GONE);
        }

        holder.evaluatePitPaperAdd.setOnClickListener(v->{
            if (beanList.size() <= 10) {
                //最多只能新增10个
                beanList.add(position + 1, baseBean);
                notifyDataSetChanged();
                notifyItemRemoved(position + 1);
            }
        });

        holder.evaluatePitPaperDelete.setOnClickListener(v->{
            if (beanList.size() > 1) {
                //留一个不删除
                beanList.remove(position);
                notifyDataSetChanged();
                notifyItemRemoved(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.evaluate_pitPaper_part)
        AppCompatSpinner evaluatePitPaperPart;
        @Bind(R.id.evaluate_pitPaper_unit)
        EditText evaluatePitPaperUnit;
        @Bind(R.id.evaluate_pitPaper_code)
        TextView evaluatePitPaperCode;
        @Bind(R.id.evaluate_pitPaper_compose)
        AppCompatSpinner evaluatePitPaperCompose;
        @Bind(R.id.evaluate_pitPaper_width)
        EditText evaluatePitPaperWidth;
        @Bind(R.id.evaluate_pitPaper_length)
        EditText evaluatePitPaperLength;
        @Bind(R.id.evaluate_pitPaper_materialNumb)
        EditText evaluatePitPaperMaterialNumb;
        @Bind(R.id.evaluate_pitPaper_price)
        EditText evaluatePitPaperPrice;
        @Bind(R.id.evaluate_pitPaper_pitPatter)
        EditText evaluatePitPaperPitPatter;
        @Bind(R.id.evaluate_pitPaper_remark)
        EditText evaluatePitPaperRemark;
        @Bind(R.id.evaluate_pitPaper_update)
        Button evaluatePitPaperUpdate;
        @Bind(R.id.evaluate_pitPaper_delete)
        Button evaluatePitPaperDelete;
        @Bind(R.id.evaluate_pitPaper_add)
        Button evaluatePitPaperAdd;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
